package com.swz.dcrm.ui.viewmodel;

import androidx.lifecycle.MediatorLiveData;

import com.swz.dcrm.api.DcrmApi;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Page;
import com.swz.dcrm.model.Schedule;
import com.swz.dcrm.util.Tool;
import com.xh.baselibrary.callback.BaseViewModel;
import com.xh.baselibrary.callback.CallBack;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Retrofit;

public class ScheduleViewModel extends BaseViewModel {

    private MediatorLiveData<BaseResponse<Page<Schedule>>> schedule;
    private MediatorLiveData<BaseResponse<Schedule>> addSchedule;
    private MediatorLiveData<BaseResponse<Object>> delSchedule;
    private Retrofit mRetrofit;
    private DcrmApi dcrmApi;
    public ScheduleViewModel(Retrofit retrofit){
        this.mRetrofit=retrofit;
        dcrmApi=mRetrofit.create(DcrmApi.class);
    }

    public MediatorLiveData<BaseResponse<Page<Schedule>>>getSchedule(String startTime,String endTime){
        schedule=creatLiveData(schedule);
        dcrmApi.getSchedule(startTime,endTime).enqueue(new CallBack<>(this,schedule));
        return schedule;
    }
    public MediatorLiveData<BaseResponse<Schedule>> addSchedule(Schedule schedule){
        addSchedule=creatLiveData(addSchedule);
        Map<String,String> map=new HashMap<>();
        try {
            map= Tool.objectToMap(schedule);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        dcrmApi.addSchedule((HashMap<String, String>) map)
                .enqueue(new CallBack<>(this,addSchedule));
        return addSchedule;
    }

    public MediatorLiveData<BaseResponse<Object>>delSchedule(long id){
        delSchedule=creatLiveData(delSchedule);
        dcrmApi.delSchedule(id)
                .enqueue(new CallBack<>(this,delSchedule));
        return delSchedule;
    }
}
